home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Libraries
/
PlayerPRO 4.4.1
/
Filters Plugs
/
Normalize.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-16
|
2KB
|
106 lines
/* Normalize */
/* v 0.2 */
/* 1995 by Liane */
// Usage:
// Another version of "Maximize", but this one
// will try to get the maximum gain.
// Works on the selected part or all the waveform
// if there is no selection.
#include "MAD.h"
#include "PPPlug.h"
#if defined(powerc) || defined(__powerc)
enum {
PlayerPROPlug = kCStackBased
| RESULT_SIZE(SIZE_CODE( sizeof(OSErr)))
| STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( Ptr*)))
| STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( struct FileInstrData*)))
| STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( long)))
| STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( long)))
| STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( PPInfoPlug*)))
};
ProcInfoType __procinfo = PlayerPROPlug;
#else
#include <A4Stuff.h>
#endif
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define labs(a) (((a) < 0) ? (-a) : (a))
OSErr main( Ptr *InstrumentPtr,
struct FileInstrData *theData,
long SelectionStart,
long SelectionEnd,
PPInfoPlug *thePPInfoPlug)
{
long i, peak = 0, temp;
if (SelectionStart == SelectionEnd) {
SelectionStart = 0;
SelectionEnd = theData->insSize;
}
switch( theData->amplitude)
{
case 8:
{
Ptr SamplePtr = (*InstrumentPtr) + SelectionStart;
for( i = 0; i < SelectionEnd - SelectionStart; i++)
{
temp = *SamplePtr++;
peak = max (peak, labs(temp));
}
if( peak != 0)
{
peak = ((long)0x80 * 0x10000) / peak;
SamplePtr = (*InstrumentPtr) + SelectionStart;
for( i = 0; i < SelectionEnd - SelectionStart; i++)
{
temp = *SamplePtr;
temp = (peak * temp) / 0x10000;
if( temp > 127) temp = 127;
else if( temp < -127 ) temp = -127;
*SamplePtr++ = temp;
}
}
} break;
case 16:
{
short *SamplePtr = (short*) *InstrumentPtr + (SelectionStart / 2);
for( i = 0; i < (SelectionEnd - SelectionStart) / 2; i++)
{
temp = (long)*SamplePtr++;
peak = max (peak, labs(temp));
}
if( peak != 0)
{
peak = ((long)0x8000 * 0x10000) / peak;
SamplePtr = (short*) *InstrumentPtr + (SelectionStart / 2);
for( i = 0; i < (SelectionEnd - SelectionStart) / 2; i++)
{
temp = (long)*SamplePtr;
temp = -(peak * temp) / 0x10000;
*SamplePtr++ = temp;
}
}
} break;
}
return noErr;
}